package com.sx.ems.controller.material;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sx.ems.entity.SysMaterial;
import com.sx.ems.service.SysMaterialService;
import com.sx.ems.utils.MsgBean;
import com.sx.ems.utils.PageUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.tomcat.jni.Local;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/material/vegetable/")
public class SysMaterialVegetableController {

    @Autowired
    private SysMaterialService sysMaterialService;

    @RequestMapping("/list")
    @RequiresPermissions("material:vegetable:list")
    public MsgBean list(@RequestParam Map<String, Object> params){
        PageUtils page = sysMaterialService.queryMaterialPage(params,2L);
        return MsgBean.ok().put("page", page);
    }

    @RequestMapping("/select")
    @RequiresPermissions("material:vegetable:select")
    public MsgBean select(){
        QueryWrapper<SysMaterial> wrapper= new QueryWrapper<>();
        wrapper.eq("mark",2L);
        List<SysMaterial> list = sysMaterialService.list(wrapper);
        return MsgBean.ok().put("list", list);
    }

    @RequestMapping("/info/{id}")
    @RequiresPermissions("material:vegetable:info")
    public MsgBean info(@PathVariable("id") Long id){
        SysMaterial material = sysMaterialService.getById(id);
        return MsgBean.ok().put("material", material);
    }

    @RequestMapping("/save")
    @RequiresPermissions("material:vegetable:save")
    public MsgBean save(@RequestBody SysMaterial material){
        material.setDate(LocalDateTime.now());
        material.setMark(2L);
        sysMaterialService.save(material);
        return MsgBean.ok();
    }

    @RequestMapping("/update")
    @RequiresPermissions("material:vegetable:update")
    public MsgBean update(@RequestBody SysMaterial material){
        material.setDate(LocalDateTime.now());
        sysMaterialService.updateById(material);
        return MsgBean.ok();
    }

    @RequestMapping("/delete")
    @RequiresPermissions("material:vegetable:delete")
    public MsgBean delete(@RequestBody Long[] ids){
        sysMaterialService.removeByIds(Arrays.asList(ids));
        return MsgBean.ok();
    }
}
